var app = getApp();
var myaudio = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 1,
    orderdetail: '',
    orderid:'',
    numcount:'',
    numcount2:'',
    sjplay:[],
    yhplay:[],
    yhplay2:[],
    timer:'',
    timer2:'',
    time1:null,
    time2:null,
    demandadd:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var demand_id = options.id;
    this.getorder(demand_id);
    this.setData({
      orderid:demand_id
    })
  },
  onshow:function(){
    
  },
  // 获取订单详情
  getorder: function (id) {
    var that = this;
    var param = {
      url: "api/shop/grabOrderDetail",
      data: {
        grab_orders_id: id,
      },
      success: function (e) {
        console.log(e);
        var yhplay = that.data.yhplay;
        var sjplay = that.data.sjplay;
        var yhplay2 = that.data.yhplay2;
        if (e.data.data.order.voice){
          var yhlen = e.data.data.order.voice.length;
          for (var i = 0; i < yhlen; i++) {
            yhplay[i] = 0;
          }
        }
        if (e.data.data.order.answer_voice){
          var sjlen = e.data.data.order.answer_voice.length;
          for (var i = 0; i < sjlen; i++) {
            sjplay[i] = 0;
          }  
        }
        if (e.data.data.demand_add){
          var yhlen2 = e.data.data.demand_add.voice.length;
          for (var i = 0; i < yhlen2; i++) {
            yhplay2[i] = 0;
          }  
        }
        e.data.data.order.status = parseInt(e.data.data.order.status);
        console.log(e.data.data.order.status)
        // if (e.data.data.status > 1){
        //   var answer_voice = e.data.data.answer_voice;
        //   var answer_voice_len = e.data.data.answer_voice_len;
        //   answer_voice = answer_voice.split(',');
        //   answer_voice_len = answer_voice_len.split(',');
        //   console.log(answer_voice);
        //   e.data.data.answer_voice = answer_voice;
        //   e.data.data.answer_voice_len = answer_voice_len;
        // }
        
        that.setData({
          orderdetail: e.data.data.order,
          demandadd:e.data.data.demand_add,
          sjplay:sjplay,
          yhplay:yhplay,
          yhplay2:yhplay2
        })
        if(e.data.data.order.status == 0){
          that.countDown(e.data.data.order.order_seconds);
        }else if (e.data.data.order.status == 1){
          clearInterval(that.data.timer)
          that.countDown2(e.data.data.order.price_seconds);
        }
        
      }

    };
    app.post(param);
  },

  // 倒计时
  countDown: function (num) {
     if (parseInt(num) > 0) {
      var time = num;
      var that = this;
      that.data.timer = setInterval(function () {
        time--;
        // 获取天、时、分、秒
        if(time > 0){
          let min = parseInt(time / 60);
          let sec = parseInt(time % 60);
          var endnum = that.timeFormat(min) + ':' + that.timeFormat(sec);
          that.setData({
            numcount: endnum,
            time1:time
          })
        }else{
          that.setData({
            time1:0
          })
          clearInterval(that.data.timer)
        }
        
      }, 1000)
    }
  },
  // 倒计时
  countDown2: function (num) {
    console.log(num)
    if (parseInt(num) > 0) {
      var time = num;
      var that = this;
      that.data.timer2 = setInterval(function () {
        time--;
        // 获取天、时、分、秒
        if(time > 0){
          let min = parseInt(time / 60);
          let sec = parseInt(time % 60);
          var endnum = that.timeFormat(min) + ':' + that.timeFormat(sec);
          that.setData({
            numcount2: endnum,
            time2:time
          })
        }else{
          that.setData({
            time2: 0
          })
          clearInterval(that.data.timer2)
        }
        
      }, 1000)
    }
  },

  //小于10的格式化函数（2变成02）
  timeFormat(param) {
    return param < 10 ? '0' + param : param;
  },
  //小于0的格式化函数（不会出现负数）
  timeFormin(param) {
    return param < 0 ? 0 : param;
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  makecall: function (e) {
    var num = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: num
    })
  },
  // 预览图片
  seeimg: function (e) {
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.orderdetail.imgs[index], // 当前显示图片的http链接  
      urls: this.data.orderdetail.imgs // 需要预览的图片http链接列表  
    })
  },

  // 抢单
  qiangdan:function(){
    var userid = wx.getStorageSync('userId');
    var orderid = this.data.orderid;
    var that = this;
    var param = {
      url:"api/shop/getOrder",
      data:{
        user_id: userid,
        grab_orders_id: orderid
      },
      success:function(e){
        console.log(e)
        wx.showToast({
          title: e.data.msg,
          icon:"none",
          duration:2000,
        })
        if(e.data.code == 1){
          wx.navigateTo({
            url: '/pages/shangjia/baojia/baojia?id='+that.data.orderid,
          })
        }
      }
    };
    app.post(param);
  },

  baojia:function(){
    wx.navigateTo({
      url: '/pages/shangjia/baojia/baojia?id='+this.data.orderid,
    })
  },

  finish:function(){
    var param = {
      url:"api/shop/completeOrder",
      data:{
        grab_orders_id:this.data.orderid,
      },
      success:function(e){
        if(e.data.code == 1){
          wx.showToast({
            title: e.data.msg,
            icon:'none',
            duration:2000
          })
          setTimeout(function(){
            wx.navigateTo({
              url: '/pages/shangjia/orderlist/orderlist',
            })
          },2000)
        }
      },

    };
    app.post(param)
    
  },

  sjvoice: function (e) {
    var src = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var sjplay = this.data.sjplay;
    var yhplay = this.data.yhplay;
    var yhplay2 = this.data.yhplay2;
    for (var i = 0; i < sjplay.length; i++) {
      sjplay[i] = 0
    }
    for (var i = 0; i < yhplay.length; i++) {
      yhplay[i] = 0
    }
    for (var i = 0; i < yhplay2.length; i++) {
      yhplay2[i] = 0
    }
    myaudio.src = src;
    if (sjplay[index] == 0) {
      myaudio.play();
      sjplay[index] = 1;
      this.setData({
        sjplay: sjplay
      })
      myaudio.onEnded(() => {
        sjplay[index] = 0;
        this.setData({
          sjplay: sjplay
        })
      })
    } else {
      myaudio.pause();
      sjplay[index] = 0;
      this.setData({
        sjplay: sjplay
      })
    }
  },
  yhvoice: function (e) {
    var src = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var yhplay = this.data.yhplay;
    var yhplay2 = this.data.yhplay2;
    var sjplay = this.data.sjplay;
    for (var i = 0; i < sjplay.length; i++) {
      sjplay[i] = 0
    }
    for (var i = 0; i < yhplay.length; i++) {
      yhplay[i] = 0
    }
    for (var i = 0; i < yhplay2.length; i++) {
      yhplay2[i] = 0
    }
    myaudio.src = src;
    if (yhplay[index] == 0) {
      myaudio.play();
      yhplay[index] = 1;
      this.setData({
        yhplay: yhplay
      })

      myaudio.onEnded(() => {
        yhplay[index] = 0;
        this.setData({
          yhplay: yhplay
        })
      })

    } else {
      myaudio.pause();
      yhplay[index] = 0;
      this.setData({
        yhplay: yhplay
      })
    }
  },

  yhvoice2: function (e) {
    var src = e.currentTarget.dataset.src;
    var index = e.currentTarget.dataset.index;
    var yhplay = this.data.yhplay;
    var yhplay2 = this.data.yhplay2;
    var sjplay = this.data.sjplay;
    for (var i = 0; i < sjplay.length; i++) {
      sjplay[i] = 0
    }
    for (var i = 0; i < yhplay.length; i++) {
      yhplay[i] = 0
    }
    for (var i = 0; i < yhplay2.length; i++) {
      yhplay2[i] = 0
    }
    myaudio.src = src;
    if (yhplay2[index] == 0) {
      myaudio.play();
      yhplay2[index] = 1;
      this.setData({
        yhplay2: yhplay2
      })

      myaudio.onEnded(() => {
        yhplay2[index] = 0;
        this.setData({
          yhplay2: yhplay2
        })
      })

    } else {
      myaudio.pause();
      yhplay2[index] = 0;
      this.setData({
        yhplay2: yhplay2
      })
    }
  },

})